package com.ecp.commons;

import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.cert.CertificateException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.context.ApplicationContext;

public class Environment implements Serializable {

	private static final long			serialVersionUID	= 1L;
	/**存储ServerAddress.properties配置文件中的公共配置项**/
	public static Map<String, String>	publicConfig		= new HashMap<String, String>();

	private Map<String, Object>			attributes;

	public static List<Integer>			canCollectMoneyArea;
	public static Environment			env;
	private ApplicationContext			appCtx;

	public static Environment getEnv() {
		if (env == null) {
			try {
				env = new Environment();
			} catch (CertificateException e) {
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return env;
	}

	public Environment() throws CertificateException, UnsupportedEncodingException, IOException, Exception {
		attributes = new ConcurrentHashMap<String, Object>();
	}

	public Object getBean(String name) {
		try {
			if (getAppCtx() != null) {
				return getAppCtx().getBean(name);
			} else {
				return null;
			}
		} catch (Exception e) {
			return null;
		}
	}

	public void setAppCtx(ApplicationContext appCtx) {
		this.appCtx = appCtx;
	}

	public ApplicationContext getAppCtx() {
		return appCtx;
	}

	public void setAttribute(String key, Object value) {
		if (null == value) {
			this.attributes.remove(key);
		} else {
			this.attributes.put(key, value);
		}
	}

	/**
	 */
	public Object getAttribute(String key) {
		return attributes.get(key);
	}

	public void initialize() throws Exception {
	}

	/*	private void logSystemProperties() {
			try {
				String path = getWebAppPath() + "/WEB-INF/ServerAddress.properties";
				InputStream is = new FileInputStream(path);
				Properties p = new Properties();
				p.load(is);
				is.close();
				
				if (p.get("server_address") != null && !p.get("server_address").toString().equalsIgnoreCase("")) {
					Global.SERVER_ADDRESS = p.get("server_address").toString();
				}
				
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}*/

	public static boolean isWindows() {
		String sysname = System.getProperty("os.name");
		if (null != sysname) {
			return sysname.toLowerCase().contains("windows");
		}
		return false;
	}

	/**
	 */
	public String getRealUploadPath() {
		return (String) this.getAttribute("RealUploadPath");
	}

	public String getContext() {
		String contextPath = (String) getAttribute("ContextPath");
		if (contextPath == null)
			return "";
		if (contextPath.equals("/")) {
			return "";
		}
		return contextPath;
	}

	public String getWebAppPath() {
		String webAppPath = (String) getAttribute("WebAppPath");
		return webAppPath;
	}

	public String getUploadPath() {

		String contextPath = (String) getAttribute("ContextPath");
		if (contextPath.length() > 0) {

			if (contextPath.startsWith("/")) {
				return contextPath + (String) getAttribute("UploadPath");
			} else {
				return "/" + contextPath + (String) getAttribute("UploadPath");
			}
		} else {
			return (String) getAttribute("UploadPath");
		}
	}

}
